package cn.bugDance.domain.activity.service;

import cn.bugDance.domain.activity.model.aggregate.ActivitySkuOrderAggregate;
import cn.bugDance.domain.activity.model.entity.ActivityCountEntity;
import cn.bugDance.domain.activity.model.entity.ActivityEntity;
import cn.bugDance.domain.activity.model.entity.ActivitySkuEntity;
import cn.bugDance.domain.activity.model.entity.ActivitySkuOrderEntity;
import cn.bugDance.domain.activity.model.valobj.ActivitySkuOrderStateVO;
import cn.bugDance.domain.activity.repository.IActivityRepository;
import cn.bugDance.domain.activity.service.policy.ITradePolicy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service
@Slf4j
public class RaffleActivityService extends AbstractRaffleActivityService{

    @Resource
    private IActivityRepository activityRepository;

    private final SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM");
    private final SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");

    public RaffleActivityService(Map<String, ITradePolicy> tradePolicyGroup) {
        super(tradePolicyGroup);
    }


    /**
     * 创建订单聚合
     * @param activitySkuEntity sku实体
     * @param activityEntity 活动实体
     * @param activityCountEntity 活动次数实体
     * @return
     */
    @Override
    protected ActivitySkuOrderAggregate buildActivitySkuOrderAggregate(ActivitySkuEntity activitySkuEntity, ActivityEntity activityEntity, ActivityCountEntity activityCountEntity, String userId, String bizId) {
        ActivitySkuOrderAggregate activitySkuOrderAggregate = new ActivitySkuOrderAggregate();
        activitySkuOrderAggregate.setActivityId(activityEntity.getActivityId());
        activitySkuOrderAggregate.setUserId(userId);
        activitySkuOrderAggregate.setSku(activitySkuEntity.getSku());
        activitySkuOrderAggregate.setActivityId(activityEntity.getActivityId());


        ActivitySkuOrderEntity activitySkuOrderEntity = new ActivitySkuOrderEntity();
        activitySkuOrderEntity.setActivityId(activityEntity.getActivityId());
        activitySkuOrderEntity.setSku(activitySkuEntity.getSku());
        activitySkuOrderEntity.setUserId(userId);
        activitySkuOrderEntity.setStrategyId(activityEntity.getStrategyId());
        activitySkuOrderEntity.setOrderId(RandomStringUtils.randomNumeric(12));
        activitySkuOrderEntity.setOutBusinessId(bizId);
        activitySkuOrderEntity.setActivityName(activityEntity.getActivityName());
        activitySkuOrderEntity.setOrderTime(new Date());
        activitySkuOrderEntity.setTotalCount(activityCountEntity.getTotalCount());
        activitySkuOrderEntity.setMonthCount(activityCountEntity.getMonthCount());
        activitySkuOrderEntity.setDayCount(activityCountEntity.getDayCount());


        activitySkuOrderAggregate.setActivityCountEntity(activityCountEntity);
        activitySkuOrderAggregate.setActivitySkuOrderEntity(activitySkuOrderEntity);
        activitySkuOrderAggregate.setMonth(monthFormat.format(activitySkuOrderEntity.getOrderTime()));
        activitySkuOrderAggregate.setDay(dayFormat.format(activitySkuOrderEntity.getOrderTime()));

        return activitySkuOrderAggregate;
    }

    /**
     * 持久化sku订单
     * @param activitySkuOrderAggregate 订单聚合
     */
    @Override
    protected void doSaveOrder(ActivitySkuOrderAggregate activitySkuOrderAggregate) {
        activityRepository.saveOrder(activitySkuOrderAggregate);
    }
}
